package o;

import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.bumptech.glide.load.engine.GlideException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

class tk<R> implements b<R>, o.aat.d {
    private static final d ˏ = new d();
    private final o.hg.e<tk<?>> ʻ;
    private boolean ʻॱ;
    private final tm ʼ;
    private boolean ʼॱ;
    private final abb ʽ;
    private th<R> ʽॱ;
    private tx<?> ʾ;
    private boolean ʿ;
    private volatile boolean ˈ;
    final e ˊ;
    private final uv ˊॱ;
    tq<?> ˋ;
    private final uv ˋॱ;
    GlideException ˎ;
    private final uv ˏॱ;
    private final uv ͺ;
    rz ॱ;
    private final AtomicInteger ॱˊ;
    private boolean ॱˋ;
    private boolean ॱˎ;
    private final d ॱॱ;
    private boolean ॱᐝ;
    private final c ᐝ;
    private sa ᐝॱ;

    class a implements Runnable {
        private final zq ˋ;
        final /* synthetic */ tk ˎ;

        a(tk tkVar, zq zqVar) {
            this.ˎ = tkVar;
            this.ˋ = zqVar;
        }

        public void run() {
            synchronized (this.ˋ.ʼ()) {
                synchronized (this.ˎ) {
                    if (this.ˎ.ˊ.ˋ(this.ˋ)) {
                        this.ˎ.ˋ.ʼ();
                        this.ˎ.ˋ(this.ˋ);
                        this.ˎ.ˏ(this.ˋ);
                    }
                    this.ˎ.ˊ();
                }
            }
        }
    }

    static final class b {
        final Executor ˋ;
        final zq ˏ;

        b(zq zqVar, Executor executor) {
            this.ˏ = zqVar;
            this.ˋ = executor;
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof b)) {
                return false;
            }
            return this.ˏ.equals(((b) obj).ˏ);
        }

        public int hashCode() {
            return this.ˏ.hashCode();
        }
    }

    class c implements Runnable {
        final /* synthetic */ tk ˋ;
        private final zq ˏ;

        c(tk tkVar, zq zqVar) {
            this.ˋ = tkVar;
            this.ˏ = zqVar;
        }

        public void run() {
            synchronized (this.ˏ.ʼ()) {
                synchronized (this.ˋ) {
                    if (this.ˋ.ˊ.ˋ(this.ˏ)) {
                        this.ˋ.ˎ(this.ˏ);
                    }
                    this.ˋ.ˊ();
                }
            }
        }
    }

    @VisibleForTesting
    static class d {
        d() {
        }

        public <R> tq<R> ॱ(tx<R> txVar, boolean z, sa saVar, c cVar) {
            return new tq(txVar, z, true, saVar, cVar);
        }
    }

    static final class e implements Iterable<b> {
        private final List<b> ˊ;

        e() {
            this(new ArrayList(2));
        }

        e(List<b> list) {
            this.ˊ = list;
        }

        void ˊ(zq zqVar, Executor executor) {
            this.ˊ.add(new b(zqVar, executor));
        }

        void ॱ(zq zqVar) {
            this.ˊ.remove(ˊ(zqVar));
        }

        boolean ˋ(zq zqVar) {
            return this.ˊ.contains(ˊ(zqVar));
        }

        boolean ˊ() {
            return this.ˊ.isEmpty();
        }

        int ˎ() {
            return this.ˊ.size();
        }

        void ॱ() {
            this.ˊ.clear();
        }

        e ˋ() {
            return new e(new ArrayList(this.ˊ));
        }

        private static b ˊ(zq zqVar) {
            return new b(zqVar, aan.ˋ());
        }

        @NonNull
        public Iterator<b> iterator() {
            return this.ˊ.iterator();
        }
    }

    tk(uv uvVar, uv uvVar2, uv uvVar3, uv uvVar4, tm tmVar, c cVar, o.hg.e<tk<?>> eVar) {
        this(uvVar, uvVar2, uvVar3, uvVar4, tmVar, cVar, eVar, ˏ);
    }

    @VisibleForTesting
    tk(uv uvVar, uv uvVar2, uv uvVar3, uv uvVar4, tm tmVar, c cVar, o.hg.e<tk<?>> eVar, d dVar) {
        this.ˊ = new e();
        this.ʽ = abb.ˊ();
        this.ॱˊ = new AtomicInteger();
        this.ˊॱ = uvVar;
        this.ͺ = uvVar2;
        this.ˋॱ = uvVar3;
        this.ˏॱ = uvVar4;
        this.ʼ = tmVar;
        this.ᐝ = cVar;
        this.ʻ = eVar;
        this.ॱॱ = dVar;
    }

    @VisibleForTesting
    synchronized tk<R> ˎ(sa saVar, boolean z, boolean z2, boolean z3, boolean z4) {
        this.ᐝॱ = saVar;
        this.ॱˋ = z;
        this.ʻॱ = z2;
        this.ॱˎ = z3;
        this.ॱᐝ = z4;
        return this;
    }

    public synchronized void ˋ(th<R> thVar) {
        this.ʽॱ = thVar;
        (thVar.ˎ() ? this.ˊॱ : ᐝ()).execute(thVar);
    }

    synchronized void ˏ(zq zqVar, Executor executor) {
        boolean z = true;
        synchronized (this) {
            this.ʽ.ˎ();
            this.ˊ.ˊ(zqVar, executor);
            if (this.ʼॱ) {
                ˏ(1);
                executor.execute(new a(this, zqVar));
            } else if (this.ʿ) {
                ˏ(1);
                executor.execute(new c(this, zqVar));
            } else {
                if (this.ˈ) {
                    z = false;
                }
                aas.ˏ(z, "Cannot add callbacks to a cancelled EngineJob");
            }
        }
    }

    @GuardedBy("this")
    void ˋ(zq zqVar) {
        try {
            zqVar.ˋ(this.ˋ, this.ॱ);
        } catch (Throwable th) {
            tb tbVar = new tb(th);
        }
    }

    @GuardedBy("this")
    void ˎ(zq zqVar) {
        try {
            zqVar.ˏ(this.ˎ);
        } catch (Throwable th) {
            tb tbVar = new tb(th);
        }
    }

    synchronized void ˏ(zq zqVar) {
        this.ʽ.ˎ();
        this.ˊ.ॱ(zqVar);
        if (this.ˊ.ˊ()) {
            ˋ();
            Object obj = (this.ʼॱ || this.ʿ) ? 1 : null;
            if (obj != null && this.ॱˊ.get() == 0) {
                ॱॱ();
            }
        }
    }

    boolean ˎ() {
        return this.ॱᐝ;
    }

    private uv ᐝ() {
        if (this.ʻॱ) {
            return this.ˋॱ;
        }
        return this.ॱˎ ? this.ˏॱ : this.ͺ;
    }

    void ˋ() {
        if (!ʽ()) {
            this.ˈ = true;
            this.ʽॱ.ˊ();
            this.ʼ.ˏ(this, this.ᐝॱ);
        }
    }

    private boolean ʽ() {
        return this.ʿ || this.ʼॱ || this.ˈ;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    void ॱ() {
        /*
        r5 = this;
        monitor-enter(r5);
        r0 = r5.ʽ;	 Catch:{ all -> 0x0024 }
        r0.ˎ();	 Catch:{ all -> 0x0024 }
        r0 = r5.ˈ;	 Catch:{ all -> 0x0024 }
        if (r0 == 0) goto L_0x0014;
    L_0x000a:
        r0 = r5.ʾ;	 Catch:{ all -> 0x0024 }
        r0.ॱॱ();	 Catch:{ all -> 0x0024 }
        r5.ॱॱ();	 Catch:{ all -> 0x0024 }
        monitor-exit(r5);	 Catch:{ all -> 0x0024 }
    L_0x0013:
        return;
    L_0x0014:
        r0 = r5.ˊ;	 Catch:{ all -> 0x0024 }
        r0 = r0.ˊ();	 Catch:{ all -> 0x0024 }
        if (r0 == 0) goto L_0x0027;
    L_0x001c:
        r0 = new java.lang.IllegalStateException;	 Catch:{ all -> 0x0024 }
        r1 = "Received a resource without any callbacks to notify";
        r0.<init>(r1);	 Catch:{ all -> 0x0024 }
        throw r0;	 Catch:{ all -> 0x0024 }
    L_0x0024:
        r0 = move-exception;
        monitor-exit(r5);	 Catch:{ all -> 0x0024 }
        throw r0;
    L_0x0027:
        r0 = r5.ʼॱ;	 Catch:{ all -> 0x0024 }
        if (r0 == 0) goto L_0x0033;
    L_0x002b:
        r0 = new java.lang.IllegalStateException;	 Catch:{ all -> 0x0024 }
        r1 = "Already have resource";
        r0.<init>(r1);	 Catch:{ all -> 0x0024 }
        throw r0;	 Catch:{ all -> 0x0024 }
    L_0x0033:
        r0 = r5.ॱॱ;	 Catch:{ all -> 0x0024 }
        r1 = r5.ʾ;	 Catch:{ all -> 0x0024 }
        r2 = r5.ॱˋ;	 Catch:{ all -> 0x0024 }
        r3 = r5.ᐝॱ;	 Catch:{ all -> 0x0024 }
        r4 = r5.ᐝ;	 Catch:{ all -> 0x0024 }
        r0 = r0.ॱ(r1, r2, r3, r4);	 Catch:{ all -> 0x0024 }
        r5.ˋ = r0;	 Catch:{ all -> 0x0024 }
        r0 = 1;
        r5.ʼॱ = r0;	 Catch:{ all -> 0x0024 }
        r0 = r5.ˊ;	 Catch:{ all -> 0x0024 }
        r0 = r0.ˋ();	 Catch:{ all -> 0x0024 }
        r1 = r0.ˎ();	 Catch:{ all -> 0x0024 }
        r1 = r1 + 1;
        r5.ˏ(r1);	 Catch:{ all -> 0x0024 }
        r1 = r5.ᐝॱ;	 Catch:{ all -> 0x0024 }
        r2 = r5.ˋ;	 Catch:{ all -> 0x0024 }
        monitor-exit(r5);	 Catch:{ all -> 0x0024 }
        r3 = r5.ʼ;
        r3.ˊ(r5, r1, r2);
        r1 = r0.iterator();
    L_0x0063:
        r0 = r1.hasNext();
        if (r0 == 0) goto L_0x007c;
    L_0x0069:
        r0 = r1.next();
        r0 = (o.tk.b) r0;
        r2 = r0.ˋ;
        r3 = new o.tk$a;
        r0 = r0.ˏ;
        r3.<init>(r5, r0);
        r2.execute(r3);
        goto L_0x0063;
    L_0x007c:
        r5.ˊ();
        goto L_0x0013;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.tk.ॱ():void");
    }

    synchronized void ˏ(int i) {
        aas.ˏ(ʽ(), "Not yet complete!");
        if (this.ॱˊ.getAndAdd(i) == 0 && this.ˋ != null) {
            this.ˋ.ʼ();
        }
    }

    void ˊ() {
        tq tqVar = null;
        synchronized (this) {
            this.ʽ.ˎ();
            aas.ˏ(ʽ(), "Not yet complete!");
            int decrementAndGet = this.ॱˊ.decrementAndGet();
            aas.ˏ(decrementAndGet >= 0, "Can't decrement below 0");
            if (decrementAndGet == 0) {
                tqVar = this.ˋ;
                ॱॱ();
            }
        }
        if (tqVar != null) {
            tqVar.ʽ();
        }
    }

    private synchronized void ॱॱ() {
        if (this.ᐝॱ == null) {
            throw new IllegalArgumentException();
        }
        this.ˊ.ॱ();
        this.ᐝॱ = null;
        this.ˋ = null;
        this.ʾ = null;
        this.ʿ = false;
        this.ˈ = false;
        this.ʼॱ = false;
        this.ʽॱ.ˋ(false);
        this.ʽॱ = null;
        this.ˎ = null;
        this.ॱ = null;
        this.ʻ.ˋ(this);
    }

    public void ॱ(tx<R> txVar, rz rzVar) {
        synchronized (this) {
            this.ʾ = txVar;
            this.ॱ = rzVar;
        }
        ॱ();
    }

    public void ˊ(GlideException glideException) {
        synchronized (this) {
            this.ˎ = glideException;
        }
        ʼ();
    }

    public void ˎ(th<?> thVar) {
        ᐝ().execute(thVar);
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    void ʼ() {
        /*
        r4 = this;
        monitor-enter(r4);
        r0 = r4.ʽ;	 Catch:{ all -> 0x001f }
        r0.ˎ();	 Catch:{ all -> 0x001f }
        r0 = r4.ˈ;	 Catch:{ all -> 0x001f }
        if (r0 == 0) goto L_0x000f;
    L_0x000a:
        r4.ॱॱ();	 Catch:{ all -> 0x001f }
        monitor-exit(r4);	 Catch:{ all -> 0x001f }
    L_0x000e:
        return;
    L_0x000f:
        r0 = r4.ˊ;	 Catch:{ all -> 0x001f }
        r0 = r0.ˊ();	 Catch:{ all -> 0x001f }
        if (r0 == 0) goto L_0x0022;
    L_0x0017:
        r0 = new java.lang.IllegalStateException;	 Catch:{ all -> 0x001f }
        r1 = "Received an exception without any callbacks to notify";
        r0.<init>(r1);	 Catch:{ all -> 0x001f }
        throw r0;	 Catch:{ all -> 0x001f }
    L_0x001f:
        r0 = move-exception;
        monitor-exit(r4);	 Catch:{ all -> 0x001f }
        throw r0;
    L_0x0022:
        r0 = r4.ʿ;	 Catch:{ all -> 0x001f }
        if (r0 == 0) goto L_0x002e;
    L_0x0026:
        r0 = new java.lang.IllegalStateException;	 Catch:{ all -> 0x001f }
        r1 = "Already failed once";
        r0.<init>(r1);	 Catch:{ all -> 0x001f }
        throw r0;	 Catch:{ all -> 0x001f }
    L_0x002e:
        r0 = 1;
        r4.ʿ = r0;	 Catch:{ all -> 0x001f }
        r0 = r4.ᐝॱ;	 Catch:{ all -> 0x001f }
        r1 = r4.ˊ;	 Catch:{ all -> 0x001f }
        r1 = r1.ˋ();	 Catch:{ all -> 0x001f }
        r2 = r1.ˎ();	 Catch:{ all -> 0x001f }
        r2 = r2 + 1;
        r4.ˏ(r2);	 Catch:{ all -> 0x001f }
        monitor-exit(r4);	 Catch:{ all -> 0x001f }
        r2 = r4.ʼ;
        r3 = 0;
        r2.ˊ(r4, r0, r3);
        r1 = r1.iterator();
    L_0x004d:
        r0 = r1.hasNext();
        if (r0 == 0) goto L_0x0066;
    L_0x0053:
        r0 = r1.next();
        r0 = (o.tk.b) r0;
        r2 = r0.ˋ;
        r3 = new o.tk$c;
        r0 = r0.ˏ;
        r3.<init>(r4, r0);
        r2.execute(r3);
        goto L_0x004d;
    L_0x0066:
        r4.ˊ();
        goto L_0x000e;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.tk.ʼ():void");
    }

    @NonNull
    public abb ˏ() {
        return this.ʽ;
    }
}
